/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageJFrame;

import ManageDAL.AccessDB;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author Pham
 */
public class JFrameLogin extends JDialog{
    JTextField txtUsername;
    JPasswordField txtPass;
    JButton btnLogin;
    JButton btnCancel;
    String user;
    public static Boolean checkLogin = false;
    
    public JFrameLogin()
    {
        
        initCreateUI();
    }

    private void initCreateUI() {
        JPanel basic = new JPanel();
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
        add(basic);
        
        JPanel pnLoginIcon = new JPanel(new BorderLayout(0, 0));
        ImageIcon iconLogin = new ImageIcon(new ImageIcon(JFrameLogin.class.getResource("../images/login.png"))
                .getImage().getScaledInstance(80, 80, Image.SCALE_SMOOTH));
        JLabel lbIcon = new JLabel(iconLogin);
        pnLoginIcon.setMaximumSize(new Dimension(400, 100));
        pnLoginIcon.add(lbIcon);
        
        basic.add(pnLoginIcon, BorderLayout.NORTH);
        
        JPanel pnInput = new JPanel(new BorderLayout(10, 10));
        pnInput.setMaximumSize(new Dimension(400, 40));
        pnInput.setBorder(BorderFactory.createEmptyBorder(15, 20, 0, 20));
        JLabel lbUsername = new JLabel("Tên đăng nhập");
        txtUsername = new JTextField();
        txtUsername.setBorder(BorderFactory.createEmptyBorder());
        txtUsername.setPreferredSize(new Dimension(250, 25));
        txtUsername.setMargin(new Insets(0, 10, 0, 0));
        pnInput.add(lbUsername, BorderLayout.WEST);
        pnInput.add(txtUsername, BorderLayout.EAST);
        
        JPanel pnPass = new JPanel(new BorderLayout(10, 10));
        pnPass.setMaximumSize(new Dimension(400, 40));
        pnPass.setBorder(BorderFactory.createEmptyBorder(15, 20, 0, 20));
        JLabel lbPass = new JLabel("Mật khẩu");
        txtPass = new JPasswordField();
        txtPass.setPreferredSize(new Dimension(250, 25));
        txtPass.setBorder(BorderFactory.createEmptyBorder());
        
        pnPass.add(lbPass, BorderLayout.WEST);
        pnPass.add(txtPass, BorderLayout.EAST);
        
        basic.add(pnInput);
        basic.add(pnPass);
        
        JPanel pnCheckRemember = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
        pnCheckRemember.setMaximumSize(new Dimension(400, 40));
        //pnCheckRemember.setBorder(BorderFactory.createEmptyBorder(10, 20, 0, 0));
        JCheckBox ckRemember = new JCheckBox("Nhớ mật khẩu");
        ckRemember.setMargin(new Insets(10, 120, 0, 0));
        pnCheckRemember.add(ckRemember);
        
        //basic.add(pnCheckRemember);
        
        JPanel pnButton = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 0));
        pnButton.setMaximumSize(new Dimension(400, 100));
        pnButton.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 10));
        btnLogin = new JButton("Đăng nhập");
        btnCancel = new JButton("Hủy bỏ");
        btnLogin.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    btnLoginactionPerformed(e);
                } catch (SQLException ex) {
                    Logger.getLogger(JFrameLogin.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        btnCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                btnCancelActionPerformed(e);
            }
        });
        
        pnButton.add(btnLogin);
        pnButton.add(btnCancel);
        this.getRootPane().setDefaultButton(btnLogin);//set btnLogin as enter key
        basic.add(pnButton);
        
        setTitle("Đăng nhập");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setModalityType(ModalityType.APPLICATION_MODAL);
    }
    
    private static void CreateAndShowUI()
    {
        JFrameLogin frame = new JFrameLogin();
        frame.setTitle("Login");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setPreferredSize(new Dimension(460, 300));
        frame.pack();
        frame.setVisible(true);
    }
    
    //btnLogin action
    private void btnLoginactionPerformed(ActionEvent e) throws SQLException {
        String username = txtUsername.getText();
        String pass = txtPass.getText();
        AccessDB access = new AccessDB();
        String queryString = "select username, password from user where username = '" + username + "'";
        ResultSet rs = access.executeQuery(queryString);
        
//            queryString = "select * from user where username = '" + username + "' and password = '" + access.getMD5(pass) +"'";
//            rs = access.executeQuery(queryString);
        
            while(rs.next())
            {
                if(rs.getString("username").equals(username)){
                    if(rs.getString("password").equals(access.getMD5(pass)))
                    {
                        user = rs.getString("username");
                        checkLogin = true;
                        JFrameMain.menuItemLogout.setEnabled(true);
                        JFrameMain.menuItemLogin.setEnabled(false);
                        JFrameMain.menuOptions.setEnabled(true);
                        
                        JOptionPane.showMessageDialog(null, "Đăng nhập thành công");
                        this.dispose();
                    }
                    
                }
                
                
           }
        if(!checkLogin)
            JOptionPane.showMessageDialog(null, "Sai thông tin đăng nhập");
    }
    
    //btnCancel action
    private void btnCancelActionPerformed(ActionEvent e)
    {
        this.dispose();
    }
    
    
    //main
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                CreateAndShowUI();
            }
        });
    }
}
